﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace FinalProject
{
    public partial class wucMyJob : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Binding(0);
            }
            else
            {
                dtlMyJob.ItemCommand += new DataListCommandEventHandler(dtlMyJob_ItemCommand);
            }
        }

        void dtlMyJob_ItemCommand(object source, DataListCommandEventArgs e)
        {
            int index = e.Item.ItemIndex;            
            if (e.CommandName == "lbtTen")
            {
                if (Utility.UserCurrent.LstCongViecUngVien[index].IsNopHoSo)
                    Response.Redirect("ChiTietNganhNghe.aspx?isNopHoSo=true&id=" + Utility.UserCurrent.LstCongViecUngVien[index].CongViecID);
                else
                    Response.Redirect("ChiTietNganhNghe.aspx?isNopHoSo=false&id=" + Utility.UserCurrent.LstCongViecUngVien[index].CongViecID +
                        "&idCongViecUngVien=" + Utility.UserCurrent.LstCongViecUngVien[index].ID);
            }
            if (e.CommandName == "lbtXoa")
            {
                Utility.UserCurrent.DeleteCongViecUngVien(Utility.UserCurrent.LstCongViecUngVien[index].ID);
                Utility.UserCurrent.LstCongViecUngVien.RemoveAt(index);
                Binding();
            }
        }

        private void Binding()
        {
            try
            {                
                if (Utility.UserCurrent.LstCongViecUngVien != null && Utility.UserCurrent.LstCongViecUngVien.Count > 0)
                {
                    dtlMyJob.DataSource = Utility.UserCurrent.LstCongViecUngVien;
                    dtlMyJob.DataBind();
                }
            }
            catch { }
        }

        public void Binding(int n)
        {
            try
            {
                if (Utility.UserCurrent.LstCongViecUngVien != null && Utility.UserCurrent.LstCongViecUngVien.Count > 0)
                {
                    if (n == 0)
                    {
                        dtlMyJob.DataSource = Utility.UserCurrent.LstCongViecUngVien;
                        dtlMyJob.DataBind();
                    }
                    else
                    {
                        List<CongViecUngVien> lst = new List<CongViecUngVien>();
                        for (int i = 0; i < Utility.UserCurrent.LstCongViecUngVien.Count; i++)
                        {
                            lst.Add(Utility.UserCurrent.LstCongViecUngVien[i]);
                            if (i == n - 1)
                                break;
                        }
                        dtlMyJob.DataSource = lst;
                        dtlMyJob.DataBind();
                    }
                }
            }
            catch { }
        }        
    }
}